package com.lfg.leetcode.leetcode0;


public class No38CountAndSay {


    public static void main(String[] args) {
        String ret = countAndSay(3);
        System.out.println(ret);
    }

    public static String countAndSay(int n) {
        String str = "1";
        for (int i = 1; i < n; i++) {
            str = count(str);
        }
        return str;
    }

    private static String count(String str) {
        StringBuilder sb = new StringBuilder();
        int count = 0;
        Character word = null;
        for (int i = 0; i < str.length(); i++) {
            if (word == null) {
                count++;
                word = str.charAt(i);
            } else if (word == str.charAt(i)) {
                count++;
            } else {
                sb.append(count);
                sb.append(word);

                count = 1;
                word = str.charAt(i);
            }
        }
        sb.append(count);
        sb.append(word);
        return sb.toString();
    }
}

